﻿package ReadWriteXML;
import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;


public class ReadXML {

	public static Document BuildXML(String path)
	{
		Document doc = null;
		DocumentBuilder builder = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try
		{
			builder = factory.newDocumentBuilder();
			doc = builder.newDocument();

		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}

		File f = new File(path);
		if(f.exists()==true)
		{
			doc = DOMUltil.parse(path);
		}
		return doc;
	}
	
	public static Document buildFromAsset(Context context, String path){
		try {
			AssetManager as = context.getAssets();
			
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder buider = factory.newDocumentBuilder();
			Document doc = buider.parse(as.open(path));
			return doc;
		} 
		catch (Exception e) {
			Log.i("Phu", "Không thể parse xml file:"+path);
		}
		return null;
	}

	public static String getTextValue(Element e, String tagName) {
		String textVal = null;
		Node parent = e.getParentNode();
		NodeList nl = e.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}
		return textVal;
	}

	/**
	 * Calls getTextValue and returns a int value
	 */
	public static int getIntValue(Element e, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(e,tagName));
	}

	
	private static COption getOption(Element e)
	{
		COption option= new COption();
		option.set_idDetail(Integer.valueOf(e.getAttribute("ID")));
		option.set_content(e.getTextContent());
		option.set_key(getTextValue(e, "Key"));
		option.set_value(getTextValue(e, "Value"));
		return option;
	}
	private static COption getOptionYN(Element e)
	{
		COption option= new COption();
		option.set_idDetail(0);
		option.set_content(e.getTextContent());
		option.set_key("");
		option.set_value("");
		return option;
	}
	private static CQuestion getQuestion(Element e)
	{
		CQuestion ques = new CQuestion();
		ArrayList<COption>optList = new ArrayList<COption>();
		ques.set_type(Integer.valueOf(e.getAttribute("Type")));
		ques.set_idQuestion(Integer.valueOf(e.getAttribute("ID")));
		ques.set_time(getIntValue(e, "Time"));
		ques.set_content(getTextValue(e, "Content"));
		ques.set_length(getIntValue(e, "Length"));
		switch(ques.get_type()){
		case 1: //câu hỏi single choice
		{
			for(int j = 0; j < ques.get_length(); j++)
			{
				Element opt= (Element)e.getChildNodes().item(3 + j);
				ques.addOption(getOption(opt));
			}
			break;
		}

		case 2: // câu hỏi multi choice
		{
			for(int j = 0; j < ques.get_length(); j++)
			{
				Element opt= (Element)e.getChildNodes().item(3 + j);
				ques.addOption(getOption(opt));
			}
			break;

		}
		
		case 3: //câu hỏi true/false
		{
			for(int j = 0; j < 2; j++)
			{
				Element opt= (Element)e.getChildNodes().item(3 + j);
				ques.addOption(getOptionYN(opt));
			}
			break;
		}
		case 4: // câu hỏi nối cột
		{
			for(int j = 0; j < ques.get_length(); j++)
			{
				COption option = new COption();
				Node listOption = e.getChildNodes().item(3 + j);

				Node key = listOption.getChildNodes().item(0);
				Node value = listOption.getChildNodes().item(1);

				String sKey = key.getTextContent();
				String sValue = value.getTextContent();

				option.set_key(sKey);
				option.set_value(sValue);
				ques.addOption(option);
			}

			break;

		}

		}
		return ques;
	}
	private static CResult getResult(Element e)
	{
		CResult res = new CResult();
		res.setIDQuestion(Integer.valueOf(e.getAttribute("IDQuestion")));
		res.setType(Integer.valueOf(e.getAttribute("Type")));

		switch (res.getType())
		{
		case 2://multi choice
		{
			Node listChoice= e.getChildNodes().item(0);
			int n = listChoice.getChildNodes().getLength();
			for(int i=0; i<n; i++)
			{
				Node choice = listChoice.getChildNodes().item(i);
				res.addChoice(choice.getTextContent());
			}
			break;
		}

		case 4://nối cột
		{
			Node listChoice= e.getChildNodes().item(0);
			int n = e.getChildNodes().getLength();
			for(int i=0; i<n; i++)
			{
				Node pChoice = listChoice.getChildNodes().item(i);
				ArrayList <String> pairChoice = new ArrayList<String>();
				pairChoice.add(pChoice.getChildNodes().item(0).getTextContent());
				pairChoice.add(pChoice.getChildNodes().item(1).getTextContent());
				res.addPairChoice(pairChoice);
			}
			break;
		}
		default:
		{
			res.addChoice(e.getChildNodes().item(0).getTextContent());
		}
		}
		return res;
	}

	private static CLearner getLearner(Element e)
	{

		CLearner learner = new CLearner();
		learner.set_id(Integer.valueOf(e.getAttribute("ID")));
		learner.set_userName(e.getAttribute("Username"));
		learner.set_pass(e.getAttribute("Pass"));

		//node fullname
		Node item= e.getChildNodes().item(0);
		learner.set_fullName(item.getTextContent());

		//node birthday
		item= e.getChildNodes().item(1);
		learner.set_birthday(item.getTextContent());

		//node address
		item= e.getChildNodes().item(2);
		learner.set_address(item.getTextContent());

		//node phone
		item= e.getChildNodes().item(3);
		learner.set_phone(item.getTextContent());

		//node id class
		item= e.getChildNodes().item(4);
		learner.set_idClass(Integer.valueOf(item.getTextContent()));

		return learner;
	}

	public static String getTextValue(String path, String tagName)
	{
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if(doc != null)
		{
			Node n = doc.getElementsByTagName(tagName).item(0);
			text = n.getTextContent();
			
		}
		 return text;
	}
	
	public static int getIntValue(String path, String tagName)
	{
		Log.d("PEERDROID","tagName: " + tagName);
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if(doc != null)
		{
			Node n = doc.getElementsByTagName(tagName).item(0);
			text = n.getTextContent();
			Log.d("PEERDROID","text: " + text);
		}
		 return Integer.valueOf(text);
	}
	
	public static String getTextValue(String path, String node, String attribute)
	{
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if(doc != null)
		{
			Element e = (Element)doc.getElementsByTagName(node).item(0);
			text = e.getAttribute(attribute);
		}
		 return text;
	}
	
	public static ArrayList<String> getTextValueElement(String path, String element, String attribute)
	{
		Document doc = null;
		doc = BuildXML(path);
		ArrayList<String> strList = new ArrayList<String>();
		
		if(doc != null)
		{
			NodeList nlist = doc.getElementsByTagName(element);
			int n = nlist.getLength();
			for(int i = 0; i < n; i++)
			{
				Element e = (Element) nlist.item(i);
				String text = e.getAttribute(attribute);
				strList.add(text);
			}
		}
		 return strList;
	}
	/**
	 * Hàm trả ra giá trị các result trong 1 file cụ thể
	 * @param doc
	 * @param path
	 * @return
	 */
	public static ArrayList<CResult> readResult(Document doc, String path)
	{
		ArrayList<CResult> resultList = new ArrayList<CResult>();
		doc = BuildXML(path);
		CResult res = null;
		NodeList resList = doc.getElementsByTagName("Result");
		int n = resList.getLength();
		for(int i = 0; i< n; i++)
		{
			Element e = (Element)resList.item(i);
			res = getResult(e);
			resultList.add(res);
		}
		return resultList;
	}

	/**
	 * Hàm trả về một result cụ thể dựa vào id và type
	 * @param doc
	 * @param path
	 * @param id
	 * @param type
	 * @return
	 */
	public static CResult readResult(Document doc, String path, int id, int type)
	{
		doc = BuildXML(path);
		CResult res = null;
		NodeList resList = doc.getElementsByTagName("Result");
		int n = resList.getLength();
		for(int i = 0; i< n; i++)
		{
			Element e = (Element)resList.item(i);
			if(e.getAttribute("IDQuestion").equals(""+id) && e.getAttribute("Type").equals(""+type))
			{	
				res = getResult(e);
				break;
			}
		}
		return res;

	}

	/**
	 * Hàm trả về một result cụ thể dựa vào id và type
	 * @param doc
	 * @param id
	 * @param type
	 * @return
	 */
	public static CResult readResult(Document doc, int id, int type)
	{
		DOMUltil.printDOM(doc);
		CResult res = null;
		NodeList resList = doc.getElementsByTagName("Result");
		int n = resList.getLength();
		for(int i = 0; i< n; i++)
		{
			Element e = (Element)resList.item(i);
			if(e.getAttribute("IDQuestion").equals(""+id) && e.getAttribute("Type").equals(""+type))
			{
				res = getResult(e);
				break;
			}

		}
		return res;

	}

	/**
	 * HÀm trả về một câu hỏi dựa vào vị trí của câu hỏi đó
	 * @param doc
	 * @param index
	 * @return
	 */
	public static CQuestion readQuestion(Document doc, int index)
	{
		NodeList quesList = doc.getElementsByTagName("Question");
		Element e = (Element)quesList.item(index);
		return getQuestion(e);

	}

	/**
	 * HÀm trả về một câu hỏi dựa vào vị trí của câu hỏi đó
	 * @param doc
	 * @param index
	 * @return
	 */
	public static CQuestion readQuestion(Document doc, String path, int index)
	{
		doc = BuildXML(path);
		NodeList quesList = doc.getElementsByTagName("Question");
		Element e = (Element)quesList.item(index);
		return getQuestion(e);

	}

	/**
	 * Hàm trả về số lượng câu hỏi trong bài tập
	 * @param doc
	 * @return
	 */
	public static int readCount(Document doc)
	{
		NodeList root = doc.getElementsByTagName("UsingQuestionList");
		Element usingQuestionList = (Element) root.item(0); 
		return getIntValue(usingQuestionList, "Count");
	}

	/**
	 * Hàm trả về số lượng câu hỏi trong bài tập
	 * @param doc
	 * @return
	 */
	public static int readCount(Document doc, String path)
	{
		doc = BuildXML(path);
		NodeList root = doc.getElementsByTagName("UsingQuestionList");
		Element usingQuestionList = (Element) root.item(0); 
		return getIntValue(usingQuestionList, "Count");
	}

	/**
	 * Hàm trả về bài tập được đọc từ 1 file cụ thể
	 * @param doc
	 * @param path
	 * @return
	 */
	public static CUsingQuestionList ReadReceive(Document doc, String path)
	{
		doc = BuildXML(path);

		CUsingQuestionList uqlist = new CUsingQuestionList();
		if(doc!=null)
		{

			NodeList root = doc.getElementsByTagName("UsingQuestionList");
			Element usingQuestionList = (Element) root.item(0); 
			uqlist.set_idDetailLearning(getIntValue(usingQuestionList, "IDDetailLearning"));
			uqlist.set_time(getIntValue(usingQuestionList, "TimeLength"));
			uqlist.set_title(getTextValue(usingQuestionList, "Title"));
			uqlist.set_idPart(getIntValue(usingQuestionList, "IDPart"));
			uqlist.set_timeBegin(getTextValue(usingQuestionList, "TimeBegin"));
			uqlist.set_status(getIntValue(usingQuestionList, "Status"));
			NodeList quesList = doc.getElementsByTagName("Question");
			int n = quesList.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element question = (Element)quesList.item(i);
					uqlist.addQuestion(getQuestion(question));
				}
			}

		}
		return uqlist;


	}

	public static ArrayList<Object> ReadInfoExercises(String path)
	{
		ArrayList<Object> arr = new ArrayList<Object>();
		Document doc = BuildXML(path);

		if(doc!=null)
		{
			NodeList root = doc.getElementsByTagName("UsingQuestionList");
			Element usingQuestionList = (Element) root.item(0); 
			arr.add(getIntValue(usingQuestionList, "IDDetailLearning"));
			arr.add(getIntValue(usingQuestionList, "TimeLength"));
			arr.add(getTextValue(usingQuestionList, "TimeBegin"));
			arr.add(getTextValue(usingQuestionList, "Title"));
			arr.add(getIntValue(usingQuestionList, "IDPart"));
			arr.add(getIntValue(usingQuestionList, "Status"));
		}
		return arr;
	}
	/**
	 * Hàm trả về bài tập được đọc từ 1 document
	 * @param doc
	 * @param fName
	 * @return
	 */
	public static CUsingQuestionList ReadReceive(Document doc)
	{
		CUsingQuestionList uqlist = new CUsingQuestionList();
		if(doc!=null)
		{

			NodeList root = doc.getElementsByTagName("UsingQuestionList");
			Element usingQuestionList = (Element) root.item(0); 
			uqlist.set_idDetailLearning(getIntValue(usingQuestionList, "IDDetailLearning"));
			uqlist.set_time(getIntValue(usingQuestionList, "TimeLength"));
			uqlist.set_timeBegin(getTextValue(usingQuestionList, "TimeBegin"));
			uqlist.set_title(getTextValue(usingQuestionList, "Title"));
			uqlist.set_idPart(getIntValue(usingQuestionList, "IDPart"));
			uqlist.set_status(getIntValue(usingQuestionList, "Status"));
			NodeList quesList = doc.getElementsByTagName("Question");
			int n = quesList.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element question = (Element)quesList.item(i);
					uqlist.addQuestion(getQuestion(question));
				}
			}

		}
		return uqlist;


	}

	public static CUsingQuestionList ReadExerciseGroup(String path)
	{
		Document doc = BuildXML(path);
		CUsingQuestionList uqlist = new CUsingQuestionList();
		if(doc!=null)
		{
			NodeList root = doc.getElementsByTagName("UsingQuestionList");
			Element usingQuestionList = (Element) root.item(0); 
			uqlist.set_idDetailLearning(getIntValue(usingQuestionList, "IDDetailLearning"));
			uqlist.set_title(getTextValue(usingQuestionList, "Title"));
			uqlist.set_idPart(getIntValue(usingQuestionList, "IDPart"));
			NodeList quesList = doc.getElementsByTagName("Question");
			int n = quesList.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element question = (Element)quesList.item(i);
					uqlist.addQuestion(getQuestion(question));
				}
			}

		}
		return uqlist;


	}
	
	/**
	 * Hàm trả về danh sách các ip từ 1 file cụ thể
	 * @param doc
	 * @param path
	 * @return
	 */
	public static ArrayList<ArrayList<String>> ReadIP(Document doc, String path)
	{

		doc = BuildXML(path);
		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();
		ArrayList<String> learnerList = new ArrayList<String>();
		if(doc!=null)
		{

			NodeList learner = doc.getElementsByTagName("Learner");
			int n = learner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)learner.item(i);
					learnerList.add(e.getAttribute("ID"));
					learnerList.add(getTextValue(e, "IP"));
					ipList.add(learnerList);
				}
			}

		}
		return ipList;
	}

	//hàm trả về danh sách ip từ file xml của giáo viên
	public static ArrayList<ArrayList<String>> ReadIPList(Document doc, String fName)
	{

		doc = BuildXML(fName);
		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();
		ArrayList<String> learnerList = new ArrayList<String>();
		if(doc!=null)
		{
			  
            NodeList learner = doc.getElementsByTagName("Learner");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		learnerList.add(e.getAttribute("ID"));
            		learnerList.add(e.getAttribute("UserName"));
            		learnerList.add(e.getAttribute("FullName"));
            		learnerList.add(getTextValue(e, "IP"));
            		learnerList.add(e.getAttribute("IDClass"));
            		ipList.add(learnerList);
            	}
            }
           
        }
		return ipList;
	}
	
	/**
	 * Hàm trả về danh sách các ip từ 1 document
	 * @param doc
	 * @return
	 */
	public static ArrayList<ArrayList<String>> ReadIP(Document doc)
	{

		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();

		if(doc!=null)
		{

			NodeList learner = doc.getElementsByTagName("Learner");
			int n = learner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)learner.item(i);
					ArrayList<String> learnerList = new ArrayList<String>();
					learnerList.add(e.getAttribute("ID"));
					learnerList.add(getTextValue(e, "IP"));
					ipList.add(learnerList);
				}
			}

		}

		return ipList;
	}
	public static ArrayList<String> ReadIP(Document doc, String fName, String sUsername)
	{
		ArrayList<String> arr = new ArrayList<String>();
		if(doc != null)
		{
			NodeList resList = doc.getElementsByTagName("Learner");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				if(e.getAttribute("UserName").equals(sUsername))
				{
				  	arr.add(getTextValue(e, "IP"));
				}
			}	
		}
		return arr;
	}
	public static ArrayList<String> ReadIP(String path)
	{
		Document doc = ReadXML.BuildXML(path);
		ArrayList<String> arr = new ArrayList<String>();
		if(doc != null)
		{
			NodeList resList = doc.getElementsByTagName("Learner");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				arr.add(getTextValue(e, "IP"));
			}	
		}
		return arr;
	}
	/**
	 * Hàm trả về danh sách các học viên từ 1 file cụ thể
	 * @param doc
	 * @param path
	 * @return
	 */
	public static ArrayList<CLearner> ReadListLearner(Document doc , String path)
	{
		doc = BuildXML(path);
		ArrayList<CLearner> learnerList = new ArrayList<CLearner>();
		if(doc!=null)
		{

			NodeList learner = doc.getElementsByTagName("Learner");
			int n = learner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)learner.item(i);
					learnerList.add(getLearner(e));
				}
			}

		}
		return learnerList;
	}

	public static ArrayList<ArrayList<String>> ReadIPLearner(Document doc)
	{

		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();
		
		if(doc!=null)
		{
			  
            NodeList learner = doc.getElementsByTagName("Learner");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		ArrayList<String> learnerList = new ArrayList<String>();
            		learnerList.add(e.getAttribute("ID"));
            		learnerList.add(e.getAttribute("UserName"));
            		learnerList.add(e.getAttribute("FullName"));
            		learnerList.add(getTextValue(e, "IP"));
            		learnerList.add(e.getAttribute("IDClass"));
            		ipList.add(learnerList);
            	}
            }
           
        }
		
		return ipList;
	}
	/**
	 * hàm trả về danh sách thông tin học viên: userName, ip, FullName
	 * @param doc
	 * @return
	 */
	public static ArrayList<String> ReadIPLearner(String path)
	{
		ArrayList<String> ipList = new ArrayList<String>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			NodeList nlearner = doc.getElementsByTagName("Learner");
			int n = nlearner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nlearner.item(i);
					ipList.add(getTextValue(e, "IP"));
				}
			}

		}

		return ipList;
	}
	
	public static CLearner ReadLearner(String fName)
	{
		Document doc = null;
		fName = Environment.getExternalStorageDirectory().toString()+ "/" +fName+".xml";
		doc = BuildXML(fName);
		CLearner learner = new CLearner();
		if(doc !=null)
		{
			Element e =(Element) doc.getElementsByTagName("Learner").item(0);
			learner = getLearner(e);
			
		}
		return learner;
	}
	
	
	/**
	 * hàm trả về danh sách thông tin học viên: CMember
	 * @param doc
	 * @return
	 */
	public static ArrayList<CMember> ReadMember(Document doc)
	{
		ArrayList<CMember> memberList = new ArrayList<CMember>();

		if(doc!=null)
		{
			NodeList nlearner = doc.getElementsByTagName("Learner");
			int n = nlearner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nlearner.item(i);
					//có 3 phần tử: [0]: userName; [1]: ip; [2]: fullname
					ArrayList<String> learner = new ArrayList<String>();
					learner.add(e.getAttribute("UserName"));
					learner.add(getTextValue(e, "IP"));
					learner.add(getTextValue(e, "FullName"));
					
					CMember mem = new  CMember();
					mem.set_fullName(getTextValue(e, "FullName"));
					mem.set_ip(getTextValue(e, "IP"));
					mem.set_userName(e.getAttribute("UserName"));
					memberList.add(mem);
				}
			}

		}

		return memberList;
	}

	public static ArrayList<CMember> ReadMember(String path)
	{
		ArrayList<CMember> memberList = new ArrayList<CMember>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			NodeList nlearner = doc.getElementsByTagName("Learner");
			int n = nlearner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nlearner.item(i);
					//có 3 phần tử: [0]: userName; [1]: ip; [2]: fullname
					ArrayList<String> learner = new ArrayList<String>();
					learner.add(e.getAttribute("UserName"));
					learner.add(getTextValue(e, "IP"));
					learner.add(getTextValue(e, "FullName"));
					
					CMember mem = new  CMember();
					mem.set_fullName(getTextValue(e, "FullName"));
					mem.set_ip(getTextValue(e, "IP"));
					mem.set_userName(e.getAttribute("UserName"));
					memberList.add(mem);
				}
			}

		}

		return memberList;
	}
	public static ArrayList<CTestofPart> ReadTestList(String path)
	{
		ArrayList<CTestofPart> testList = new ArrayList<CTestofPart>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			NodeList nTest = doc.getElementsByTagName("Test");
			int n = nTest.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nTest.item(i);
					CTestofPart test = new CTestofPart();
					int idPart = Integer.valueOf(e.getAttribute("IDPart"));
					int idDetailLearning = Integer.valueOf(e.getAttribute("IDDetailLearning"));
					String nameTest= e.getChildNodes().item(0).getTextContent();
					String title = e.getChildNodes().item(1).getTextContent();

					test.set_idPart(idPart);
					test.addNameTest(nameTest);
					test.addTitleNameTest(title);
					test.set_idDetailLearning(idDetailLearning);
					testList.add(test);
				}
			}

		}

		return testList;

	}
	public static String ReadTestNew(String path)
	{
		String nameTest = null;
		Document doc = BuildXML(path);
		if(doc !=null)
		{
			NodeList nTest = doc.getElementsByTagName("Test");
			int n = nTest.getLength();
			Element e = (Element)nTest.item(n - 1);
			nameTest= e.getChildNodes().item(0).getTextContent();
		}
		return nameTest;
	}
	
	public static ArrayList<String> ReadNameExerciseInTestList(String path)
	{
		ArrayList<String> testList = new ArrayList<String>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			NodeList nTest = doc.getElementsByTagName("Test");
			int n = nTest.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nTest.item(i);
					String nameExercise = getTextValue(e,"NameTest");
					testList.add(nameExercise);
				}
			}

		}

		return testList;
	}
	
	public static ArrayList<Object> ReadTestListGroup(String path)
	{
		ArrayList<Object> testList = new ArrayList<Object>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			Node idPart = doc.getElementsByTagName("IDPart").item(0);
			testList.add(Integer.valueOf(idPart.getTextContent()));
			Node idDetailLearning = doc.getElementsByTagName("IDDetailLearning").item(0);
			testList.add(Integer.valueOf(idDetailLearning.getTextContent()));
			Node idExercise = doc.getElementsByTagName("IDExercise").item(0);
			testList.add(Integer.valueOf(idExercise.getTextContent()));
			Node title = doc.getElementsByTagName("Title").item(0);
			testList.add(title.getTextContent());
		}
		return testList;
	}
	public static ArrayList<CTestofPart> ReadTestList(String path, int idPart, int idDetailLearning)
	{
		ArrayList<CTestofPart> testList = new ArrayList<CTestofPart>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			NodeList nTest = doc.getElementsByTagName("Test");
			int n = nTest.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nTest.item(i);
					int idPartTest = Integer.valueOf(e.getAttribute("IDPart"));
					int idDetailLearningTest = Integer.valueOf(e.getAttribute("IDDetailLearning"));
					if(idPart == idPartTest && idDetailLearning == idDetailLearningTest)
					{
						CTestofPart test = new CTestofPart();
						String nameTest= e.getChildNodes().item(0).getTextContent();
						String title = e.getChildNodes().item(1).getTextContent();

						test.set_idPart(idPart);
						test.addNameTest(nameTest);
						test.addTitleNameTest(title);
						test.set_idDetailLearning(idDetailLearning);
						testList.add(test);
					}
				}
			}

		}

		return testList;

	}
	public static ArrayList<ArrayList<String>> ReadQuestion(String path)
	{
		ArrayList<ArrayList<String>> quesList = new ArrayList<ArrayList<String>>();
			Document doc = BuildXML(path);
			if(doc!=null)
			{
				NodeList nlQues = doc.getElementsByTagName("Question");
				int n = nlQues.getLength();
				if(n!= 0)
				{
					for(int i=0; i<n; i++)
					{
						Element e = (Element)nlQues.item(i);
						ArrayList<String> ques = new ArrayList<String>();
						
						int type = getIntValue(e, "Type");
						ques.add(String.valueOf(type));
						ques.add(getTextValue(e, "Content"));
						
						NodeList nlOpt = e.getChildNodes();
						int length = nlOpt.getLength();
						
						for(int j = 0; j < length; j++)
						{
							Element opt= (Element)nlOpt.item(j);
							if(type !=5)
							{
								ques.add(opt.getAttribute("isAnswer"));
							}
							ques.add(opt.getTextContent());
						}
						quesList.add(ques);
					}
				}

			}

			return quesList;
	}
	
	public static ArrayList<Object> ReadResultTest(Document doc, int idQuestion, int type)
	{
		ArrayList<Object> res = new ArrayList<Object>();
		if(doc != null)
		{
			Node parent = doc.getElementsByTagName("ResultTest").item(0);
			
			NodeList resList = parent.getChildNodes();
			int n = resList.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)resList.item(i);
					if(e.getAttribute("IDQuestion").equals(""+idQuestion) && e.getAttribute("Type").equals(""+type) )
					{	
//						res.add(idQuestion);
//						res.add(type);
						
						NodeList valueList = e.getChildNodes();
						int size = valueList.getLength();
						for(int j = 0; j < size; j++)
						{
							res.add(valueList.item(j).getTextContent());
						}
						break;
					}
				}
			}
		}
		return res;
	}
	
	public static ArrayList<Object> ReadResultTest(String path, int idQuestion)
	{
		ArrayList<Object> res = new ArrayList<Object>();
		Document doc = BuildXML(path);
		if(doc != null)
		{
			Node parent = doc.getElementsByTagName("ResultTest").item(0);
			
			NodeList resList = parent.getChildNodes();
			int n = resList.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)resList.item(i);
					if(e.getAttribute("IDQuestion").equals(""+idQuestion))
					{	
						res.add(idQuestion);
						
						int type = Integer.valueOf(e.getAttribute("Type"));
						res.add(type);
						
						NodeList valueList = e.getChildNodes();
						int size = valueList.getLength();
						for(int j = 0; j < size; j++)
						{
							res.add(valueList.item(j).getTextContent());
						}
						break;
					}
				}
			}
		}
		return res;
	}

	
}

